/**
 * 
 */
package no.mehdi.game.service;

import no.mehdi.game.domain.Player;
import no.mehdi.game.repository.PlayerRepository;

import org.springframework.security.authentication.encoding.PasswordEncoder;

/**
 * The PlayerServices implementation.
 * 
 * @author Mehdi Zare
 * 
 */
public class PlayerServicesImpl implements PlayerServices {
	/** The playerRepository injected by Spring */
	private PlayerRepository playerRepository;
	private PasswordEncoder passwordEncoder;

	/*
	 * (non-Javadoc)
	 * 
	 * @see no.mehdi.game.service.PlayerServices#addPlayer(java.lang.String)
	 */
	@Override
	public Player addPlayer(String playerName, String password) {
		Player player = playerRepository.getPlayerByName(playerName);
		if (player == null) {
			Player newPlayer = new Player();
			newPlayer.setPlayerName(playerName);
			newPlayer.setPassword(passwordEncoder.encodePassword(password, null));
			playerRepository.savePlayer(newPlayer);
			return newPlayer;
		}
		return player;

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * no.mehdi.game.service.PlayerServices#updatePlayerResults(no.mehdi.game
	 * .domain.Player)
	 */
	@Override
	public void updatePlayerResults(Player player) {
		playerRepository.savePlayer(player);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * no.mehdi.game.service.PlayerServices#retrievePlayerByName(java.lang.String
	 * )
	 */
	@Override
	public Player retrievePlayerByName(String playerName) {
		return playerRepository.getPlayerByName(playerName);

	}

	/**
	 * @param playerRepository
	 *            the playerRepository to set
	 */
	public void setPlayerRepository(PlayerRepository playerRepository) {
		this.playerRepository = playerRepository;
	}

	/**
	 * @param passwordEncoder
	 *            the passwordEncoder to set
	 */
	public void setPasswordEncoder(PasswordEncoder passwordEncoder) {
		this.passwordEncoder = passwordEncoder;
	}

}
